/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer 66. 构建乘积数组
 * @author: WXY
 * @create: 2022-12-11 15:57
 * @Version 1.0
 **/
public class Num066_constructArr {
    public static int[] constructArr(int[] a) {
        if (a.length == 0) return a;
        int[] left = new int[a.length];
        int[] right = new int[a.length];
        left[0] = a[0];
        right[a.length - 1] = a[a.length - 1];
        for (int i = 1; i < a.length; i++) {
            left[i] = a[i] * left[i - 1];
        }
        for (int i = a.length - 2; i >= 0; i--) {
            right[i] = a[i] * right[i + 1];
        }
        a[0] = right[1];
        a[a.length - 1] = left[a.length - 2];
        for (int i = 1; i < a.length - 1; i++) {
            a[i] = left[i - 1] * right[i + 1];
        }
        return a;
    }
}
